package com.hb.bussiness.dto;


import com.hb.bussiness.enums.ErrorCodeEnum;

import java.io.Serializable;

/**
 * es响应
 *
 * @author hb
 * @date 2023/04/25
 */
public class CommonResponse<T> implements Serializable {
    private Integer code;
    private String message;
    private T data;

    public CommonResponse(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public CommonResponse(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> CommonResponse<T> success(T data) {
        return new CommonResponse(ErrorCodeEnum.SUCCESS.getCode(), ErrorCodeEnum.SUCCESS.getMsg(), data);
    }

    public static CommonResponse success() {
        return new CommonResponse(ErrorCodeEnum.SUCCESS.getCode(), ErrorCodeEnum.SUCCESS.getMsg());
    }

    public static <T> CommonResponse<T> fail() {
        return new CommonResponse(ErrorCodeEnum.FAIL.getCode(), ErrorCodeEnum.FAIL.getMsg());
    }

    public static <T> CommonResponse<T> fail(ErrorCodeEnum errorCodeEnum) {
        return new CommonResponse(errorCodeEnum.getCode(), errorCodeEnum.getMsg());
    }

    public static <T> CommonResponse<T> fail(ErrorCodeEnum errorCodeEnum, String exception) {
        return new CommonResponse(errorCodeEnum.getCode(), errorCodeEnum.getMsg() + exception);
    }

    public CommonResponse() {
        this.code = ErrorCodeEnum.SUCCESS.getCode();
        this.message = ErrorCodeEnum.SUCCESS.getMsg();
    }

    public Integer getCode() {
        return this.code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "CommonResponse{code=" + this.code + ", message='" + this.message + '\'' + ", data=" + this.data + '}';
    }
}
